package com.cg.springSecurity.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisUtils{

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public boolean hasKey(final String key){
        return redisTemplate.hasKey(key);
    }

    public boolean removeKey(String key) {
        return redisTemplate.delete(key);
    }
    /**
     * 根据key读取数据
     */
    public Object get(final String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取key剩余存活时间(默认秒)
    public long getExpire(final String key){
        return redisTemplate.opsForValue().getOperations().getExpire(key);
    }

    //获取key剩余存活时间(自定义返回单位)
    public long getExpire(final String key , TimeUnit unit){
        return redisTemplate.opsForValue().getOperations().getExpire(key , unit);
    }

    //获取剩余存活时间并格式化
    public String getExpireFormat(final String key){
        long expire = getExpire(key , TimeUnit.MILLISECONDS);
        long day = TimeUnit.MILLISECONDS.toDays(expire);

        long hours = TimeUnit.MILLISECONDS.toHours(expire)
                - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(expire));

        long minutes = TimeUnit.MILLISECONDS.toMinutes(expire)
                - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(expire));

        long seconds = TimeUnit.MILLISECONDS.toSeconds(expire)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(expire));

        long ms = TimeUnit.MILLISECONDS.toMillis(expire)
                - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(expire));

        return String.format("%d 天 %d 时 %d 分 %d 秒 %d 毫秒", day, hours, minutes, seconds ,ms);
    }
    /**
     * 写入数据
     */
    public boolean set(final String key, Object value) {
        if (StringUtils.isBlank(key)) {
            return false;
        }
        try {
            redisTemplate.opsForValue().set(key, value);
//            log.info("存入redis成功，key：{}，value：{}", key, value);
            return true;
        } catch (Exception e) {
            log.error("存入redis失败，key：{}，value：{}", key, value);
            e.printStackTrace();
        }
        return false;
    }

    //写入数据并设置过期时间,单位为毫秒
    public boolean set(final String key, Object value , long time) {
        if (StringUtils.isBlank(key)) {
            return false;
        }
        try {
            redisTemplate.opsForValue().set(key, value , time , TimeUnit.MILLISECONDS);
//            redisTemplate.expire(key , time , TimeUnit.MILLISECONDS);
            log.info("存入redis成功，key：{}，value：{} , 有效时间为：{}毫秒", key, value , time);
            return true;
        } catch (Exception e) {
            log.error("存入redis失败，key：{}，value：{} , 有效时间为：{}毫秒", key, value ,time);
            e.printStackTrace();
        }
        return false;
    }

    //写入数据并设置过期时间,自定义时间单位
    public boolean set(final String key, Object value , long time , TimeUnit unit) {
        if (StringUtils.isBlank(key)) {
            return false;
        }
        try {
            redisTemplate.opsForValue().set(key, value);
            redisTemplate.expire(key , time , unit);
            log.info("存入redis成功，key：{}，value：{} , 有效时间为：{}", key, value , time);
            return true;
        } catch (Exception e) {
            log.error("存入redis失败，key：{}，value：{} , 有效时间为：{}", key, value ,time);
            e.printStackTrace();
        }
        return false;
    }

}
